// 2013-9-12上午02:26:25  net.betou.trade.main.manager.agencyorproxy.impl.SystemMngImpl.java

package net.betou.trade.main.manager.agencyorproxy.impl;

import net.betou.common.hibernate3.Updater;
import net.betou.common.page.Pagination;
import net.betou.common.security.encoder.PwdEncoder;
import net.betou.common.security.encoder.SaltGenerator;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.AgentAndAdminsExt;
import net.betou.trade.main.dao.agencyorproxy.SystemAdminDao;
import net.betou.trade.main.dao.role.SystemRoleDao;
import net.betou.trade.main.manager.AgentAndAdminsExtMng;
import net.betou.trade.main.manager.agencyorproxy.SystemAdminMng;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author 叶装装
 */
@Service
@Transactional
public class SystemMngImpl implements SystemAdminMng{
	
	@Transactional( readOnly = true )
	public Pagination getPage( String searchProperty, String searchValue, String orderProperty, String orderDirection, int pageNo, int pageSize, String filterStatus ) {
		return dao.getPage( true, null, null, null, searchProperty, searchValue, orderProperty, orderDirection, pageNo, pageSize, filterStatus ); // 系统管理员，只需要直接传入System
	}
	
	@Transactional( readOnly = true )
	public AgentAndAdmins findById( Integer id ){
		return dao.findById( id );
	}
	
	@Transactional( readOnly = true )
	public AgentAndAdmins findBySystem( Integer id ){
		return dao.findBySystem( id );
	}
	
	public AgentAndAdmins [ ] deleteByIds( Integer [ ] ids ){
		AgentAndAdmins [ ] beans = new AgentAndAdmins [ ids.length ];
		for ( int i = 0, len = ids.length; i < len; i++ ){
			beans[ i ] = dao.deleteById( ids[ i ] );
		}
		return beans;
	}
	
	public AgentAndAdmins update( Boolean disabled, String password, String email, AgentAndAdmins bean, AgentAndAdminsExt ext, Integer [ ] roleIds ) {

		if( StringUtils.isNotBlank( password ) ){
			String loginSalt = salt.randomSalt( ); // 重新生成密码混淆

			bean.setSalt( loginSalt );
			bean.setPassword( pwdEncoder.encodePassword( password, loginSalt ) );
		}

		if( disabled == null ){
			bean.setDisabled( true );
		}else{
			bean.setDisabled( false );
		}

		bean.setEmail( StringUtils.isBlank( email ) ? null : email ); // 设置成null

		bean.getRoles( ).clear( ); // 清空角色

		if( roleIds != null ){
			for ( Integer id : roleIds ){
				bean.addToRoles( roleDao.findById( id ) );
			}
		}

		Updater< AgentAndAdmins > updater = new Updater< AgentAndAdmins >( bean );
		updater.include( "email" );
		bean = dao.updateByUpdater( updater ); // 更新主表
		extMng.update( ext, bean ); // 更新附表

		return bean;
	}
	
	public AgentAndAdmins save( String ip, Integer [ ] roleIds, AgentAndAdmins bean, AgentAndAdminsExt ext ) {

		bean.setCreateIp( ip );
		String loginSalt = salt.randomSalt( );

		bean.setSalt( loginSalt );
		bean.setPassword( pwdEncoder.encodePassword( bean.getPassword( ), loginSalt ) );

		if( roleIds != null ){
			for ( Integer id : roleIds ){
				bean.addToRoles( roleDao.findById( id ) );
			}
		}
		
		bean.saveToSystem( );

		dao.save( bean );
		extMng.save( ext, bean );

		return bean;

	}

	@Autowired
	private SystemAdminDao dao;
	
	@Autowired
	private SystemRoleDao roleDao;
	
	@Autowired
	private SaltGenerator salt;
	
	@Autowired
	private PwdEncoder pwdEncoder;
	
	@Autowired
	private AgentAndAdminsExtMng extMng;
}
